package com.rrd.hive.udf;

import com.rrd.hive.util.DateUtils;
import com.rrd.hive.util.IPLocation;
import com.rrd.hive.util.Location;
import com.weicai.caesar.CaesarUtil;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Description(
        name = "desensitization",
        value = "_FUNC_(name) - 获取脱敏后得数据 1:姓名、2:身份证、3:手机号、4:银行卡号、5:电话、6:地址",
        extended = "Example:\n> SELECT _FUNC_(1,'张三');\n "
)
/**
 * @author baoxin.liu
 * 支持对姓名、身份证、手机号、银行卡部分字符进行掩码
 */
public class DesensitizationUDF extends UDF {
    public static final   Pattern Number_pattern = Pattern.compile("[0-9]+(X|x)?");
    public static final   Pattern Address_pattern = Pattern.compile(".*[0-9]+(楼|号楼|院)(.*)");
    private Text text = new Text();

    private String generatePass(){
        String s="yjj"+ DateUtils.formateDateStr(new Date(),"yyyyMMdd")+"%#32";
        return s;
    }
    public Text evaluate(String str) throws Exception {
        if(str == null || str.equals("")){
            text.set("");
            return text;
        }
        text.set(takoff(str));
        return text;
    }

   private String takoff(String str){
       String s= "";
       if (CaesarUtil.isEncrypted(str)) {
           s = CaesarUtil.decode(str);
       } else {
           s = str;
       }

       Matcher m = Number_pattern.matcher(s);
       if (m.matches()) {
           if (s.length()==15 || s.length()==18) {   //身份证
               s = s.substring(0, 6) + "******" + s.substring(12);
           } else if (s.length()==11) {  //手机号
               s = s.substring(0, 4) + "****" + s.substring(8);
           } else if (s.length() >= 13 && s.length() <=19) {  //银行卡
               s = "********" + s.substring(s.length() - 4);
           } else if (s.length()>=5 && s.length()<=11) {  //电话
               s = s.substring(0, 5) + "****";
           }else{
               s=str;
           }
       }else{
           if(s.length()<=10){
               //脱敏姓名：
               if(s.length()==2){
                   s = s.substring(0,1)+"*";
               }else if(s.length()>=3){
                   s = s.substring(0,1)+"*"+s.substring(2);
               }
           }else{
                m = Address_pattern.matcher(s);
               if (m.find()) {
                   s = s.replace(m.group(2), "****");
               }else{
                   s=str;
               }
           }
       }
        return s;
   }

    public static void main(String[] args) throws Exception {
        DesensitizationUDF des = new DesensitizationUDF();
        String s="53242819930206032X";
       System.out.println("abc:"+des.evaluate(s));
       System.out.println(CaesarUtil.decode(s));


    }
}
